﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class update_psw : System.Web.UI.Page
{
    DataClassesDataContext db1 = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox2.Text.ToString().Trim().Length < 4)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>window.alert('密码太短，为确保账户安全请重新设置！');</script>");
                return;
            }
            int type =Convert.ToInt32(Session["type"].ToString());
            
          
            string pwd = TextBox2.Text;
            string phone = "";
            string oldPwd = TextBox1.Text;
            phone=(string)Session["phone"];
            string wbh = "";
            int? username = null;
        
            if (type == 0)
            {
                wbh = Session["wbh"].ToString();
            }
        else
            {
                username =Convert.ToInt32(Session["username"].ToString());
            }
            if (db1.proc_LoginCheck(phone, oldPwd) == 0)
            {
                if (db1.proc_UpdateStuPwd(wbh, pwd) == -1)
                    Response.Write("<script>alert('原密码输入错误');location.href='ModifyPwd.aspx'</script>");
                else
                {
                    db1.SubmitChanges();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "Success", "<script>alert('修改成功，请用新密码重新登陆！');window.parent.parent.location='login.aspx'</script>");
                }
            }
            else if (db1.proc_LoginCheck(phone, oldPwd) ==1)
            {

                if (db1.proc_UpdateAdminPwd(username, pwd) == -1)
                {
                    Response.Write("<script>alert('修改失败');location.href='ModifyPwd.aspx'</script>");
                }
                else
                {
                    db1.SubmitChanges();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "Success", "<script>alert('修改成功，请用新密码重新登陆！');window.parent.parent.location='login.aspx'</script>");
                }
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('密码输入错误！');</script>");
                return;
            }
        }
    }
    
